var nickNameKey = "nickName";
var userIdKey = "userId";
var roomNumberKey = "roomNumber";

function writeUserInfo() {
	var infoStr = '';
	var nickName = getCookie(nickNameKey);
	if (nickName != null && nickName.length > 0) {
		infoStr = '<div id="userInfo">';
		infoStr += '<h3 style="line-height:20px;">Welcome：' + nickName
				+ '<i class="icon-edit"></i><h3>';
	} else {
		infoStr = '<div id="userInfo" class="input-append">';
		infoStr += '<input id="nicknameInput" type="text" placeholder="请输入昵称……">';
		infoStr += '<button class="btn btn-success" type="button" onclick="saveNickname();">确认</button>';
	}
	infoStr += '</div>';
	document.write(infoStr);
}

function generateRandomString() {
	var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
	var maxPos = chars.length - 1;
	var randomString = '';
	for (var i = 0; i < 32; i++) {
		randomString += chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return randomString;
}

function saveNickname() {
	var nickName = document.getElementById("nicknameInput").value;
	var userId = generateRandomString();
	addCookie(userIdKey, userId, 365 * 24);
	addCookie(nickNameKey, nickName, 365 * 24);
	window.location.reload();
}

function getLocalUserData() {
	var nickName = getCookie(nickNameKey);
	var userId = getCookie(userIdKey);
	var userData = {
		nickName : nickName,
		userId : userId
	};
	return userData;
}

function createNewRoom() {
	$.ajax({
				type : "POST",
				url : "createRoom.do",
				data : getLocalUserData(),
				success : function(result) {
					addCookie(roomNumberKey, result, 1);
					showGameRoomInfo();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert("发生异常,请稍候再试" + errorThrown);
				}
			});
}

function showGameRoomInfo() {
	var roomNumber = getCookie(roomNumberKey);
	var roomInfo = "<dl><dt>房间号</dt><dd>" + roomNumber + "</dd>";
	roomInfo += "<dt>已加入玩家</dt><dd></dd>";
	document.getElementById("roomInfoShow").innerHTML = roomInfo;
	document.getElementById("roomInfo").style.display = "block";
	document.getElementById("createRoom").style.display = "none";
}

function writeGameRoomInfo() {
	var roomNumber = getCookie(roomNumberKey);
	if (roomNumber == null || roomNumber.length == 0) {
		document.getElementById("createRoom").style.display = "block";
	} else {
		showGameRoomInfo();
	}
}

function refreshUsers() {
	var roomNumber = getCookie(roomNumberKey);
}

function joinGameRoom() {
	var data = getLocalUserData();
	data.roomNumber = document.getElementById("roomId").value;
	$.ajax({
				type : "POST",
				url : "joinGameRoom.do",
				data : getLocalUserData(),
				success : function(result) {
					if (result == "unexist") {
						alert("房间号不存在，请确认后再输入。");
					} else {
						addCookie(roomNumberKey, result, 1);
						showGameUserRoomInfo();
					}

				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert("发生异常,请稍候再试" + errorThrown);
				}
			});
}

function showGameUserRoomInfo() {
	var roomNumber = getCookie(roomNumberKey);
	document.getElementById("joinGameRoom").style.display = "none";
	document.getElementById("joinGameRoomInfo").style.display = "block";
	document.getElementById("showJoinRoomInfo").innerHTML = "已加入房间:"
			+ roomNumber;

}

function writeGameUserRoomInfo() {
	var roomNumber = getCookie(roomNumberKey);
	if (roomNumber != null || roomNumber.length > 0) {
		showGameUserRoomInfo();
	}
}